<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <img src="../../assets/logo.png" alt="Logo" class="logo" />
        <h1 class="title">贵州四季康养管理系统</h1>
      </div>
      <t-form
        ref="form"
        class="login-form"
        :data="formData"
        :rules="rules"
        @submit="onSubmit"
      >
        <t-form-item name="username">
          <t-input
            v-model="formData.username"
            size="large"
            placeholder="请输入用户名"
          >
            <template #prefix-icon>
              <t-icon name="user" />
            </template>
          </t-input>
        </t-form-item>
        <t-form-item name="password">
          <t-input
            v-model="formData.password"
            type="password"
            size="large"
            placeholder="请输入密码"
          >
            <template #prefix-icon>
              <t-icon name="lock-on" />
            </template>
          </t-input>
        </t-form-item>
        <div class="login-actions">
          <t-checkbox v-model="rememberMe">记住我</t-checkbox>
          <t-link theme="primary" hover="color" size="small">忘记密码？</t-link>
        </div>
        <t-form-item>
          <t-button
            type="submit"
            theme="primary"
            size="large"
            block
            :loading="loading"
          >
            登录
          </t-button>
        </t-form-item>
      </t-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { MessagePlugin } from 'tdesign-vue-next'
import type { FormRule } from 'tdesign-vue-next'
import { login } from '../../api/auth'

const router = useRouter()
const route = useRoute()
const loading = ref(false)
const rememberMe = ref(false)

// 表单数据
const formData = reactive({
  username: '',
  password: ''
})

// 表单校验规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', type: 'error', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', type: 'error', trigger: 'blur' },
    { min: 6, message: '密码长度至少6位', type: 'error', trigger: 'blur' } as FormRule
  ]
}

// 处理表单提交
const onSubmit = async ({ validateResult }: { validateResult: boolean }) => {
  if (!validateResult) return
  
    loading.value = true
    
    try {
    const data: any = await login(formData)
      
      // 存储token
    localStorage.setItem('token', data.token)
      
    // 记住用户信息
      if (rememberMe.value) {
      localStorage.setItem('username', formData.username)
    } else {
      localStorage.removeItem('username')
      }
      
    // 提示登录成功
      MessagePlugin.success('登录成功')
    
    // 跳转页面
    const redirect = route.query.redirect as string
    router.push(redirect || '/dashboard')
    } catch (error) {
    console.error('登录失败', error)
    // 错误已在请求拦截器处理
    } finally {
      loading.value = false
    }
  }

// 从本地存储加载登录信息
const initLoginInfo = () => {
  const savedUsername = localStorage.getItem('username')
  if (savedUsername) {
    formData.username = savedUsername
    rememberMe.value = true
  }
}

// 初始化
initLoginInfo()
</script>

<style scoped>
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background-color: #f7f9fc;
  background-image: url('../../assets/login-bg.jpg');
  background-size: cover;
  background-position: center;
}

.login-box {
  width: 400px;
  padding: 40px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.08);
}

.login-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 32px;
}

.logo {
  width: 80px;
  height: 80px;
  margin-bottom: 16px;
}

.title {
  font-size: 24px;
  font-weight: 600;
  color: #0052D9;
  margin: 0;
}

.login-form {
  margin-bottom: 16px;
}

.login-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}
</style> 